package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.NavModel;
import com.wanxi.service.NavService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Li
 * @Date 2023/7/9 9:44
 */
@RestController
@RequestMapping("/back/nav")
@Api(tags = "导航信息管理",value = "Open Api")
public class NavController {
    @Autowired
    private NavService navService;
    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询导航信息",response = NavModel.class,httpMethod = "GET")
    public ResultDto findAll(NavModel navModel){
        Page<NavModel> page = new Page<>(navModel.getPage(),navModel.getLimit());
        QueryWrapper<NavModel> queryWrappers = query(navModel);
        IPage<NavModel> iPage = navService.page(page,queryWrappers);
        return ResultDto.builder()
                .code(0)
                .data(iPage.getRecords())
                .count((int) iPage.getTotal())
                .build();
    }



    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询导航信息",response = NavModel.class,httpMethod = "GET")
    public ResultDto finById(NavModel navModel){
       NavModel nav = navService.getById(navModel.getId());
        return ResultDto.builder()
                .code(0)
                .data(nav)
                .build();
    }

    @PostMapping("/addNav")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增导航信息",response = NavModel.class,httpMethod = "POST")
    public ResultDto addNav(NavModel navModel){
        navService.save(navModel);
        return ResultDto.builder()
                .code(0)
                .msg("新增成功！")
                .build();
    }

    @PostMapping("/updateNav")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改导航信息",response = NavModel.class,httpMethod = "POST")
    public ResultDto updateNav(NavModel navModel){
        navService.updateById(navModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功！")
                .build();
    }

    @PostMapping("/deleteNav")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除导航信息",response = NavModel.class,httpMethod = "POST")
    public ResultDto deleteNav(NavModel navModel){
        navService.removeById(navModel.getId());
        return ResultDto.builder()
                .code(0)
                .msg("删除成功！")
                .build();
    }

    private QueryWrapper<NavModel> query(NavModel navModel) {
        QueryWrapper<NavModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(navModel.getNavName()==null),"nav_name",navModel.getNavName());
        queryWrapper.eq(navModel.getEnable()!=null&&navModel.getEnable()!=-1,"enable",navModel.getEnable());
        return queryWrapper;
    }
}
